#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>
#include <unistd.h>


int fd;

void signal_fun(int signum)
{
    unsigned char key_val;
    read(fd, &key_val, 1);
    printf("signal = %d key_val = %x\n", signum, key_val);
}

int main(int argc, char **argv)
{
    int oflags;

    //注册信号处理函数
    signal(SIGIO, signal_fun);

    fd = open("/dev/key0", O_RDWR);
    if(fd < 0)
    {
        printf("can't open!\n");
        return 0;
    }

    //获取进程pid并告知驱动程序
    fcntl(fd, F_SETOWN, getpid());
    oflags = fcntl(fd, F_GETFL);
    fcntl(fd, F_SETFL, oflags | FASYNC);

    while(1)
    {
        sleep(1);
    }

    return 0;
}
